package com.unnamed.collisions.geometry;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Shapecosa {
	List<Polygon> polygons;
	CollisionHandler handler;
	List<Collision> collisions;
	List<List<Point>> colP;
	boolean colliding;
	PolygonCollider[] colliders = new PolygonCollider[2];
	
	public Shapecosa(){
		polygons = new ArrayList<Polygon>();
		collisions = new ArrayList<Collision>();
		colP = new ArrayList<List<Point>>();
		for(int c=0;c<colliders.length;c++){
			colliders[c]=new PolygonCollider();
		}
	}
	
	public void setHandler(CollisionHandler handler){
		this.handler = handler;
	}
	public void add(Polygon poly){
		this.polygons.add(poly);
	}
	
	public void collide(){
		synchronized (this) {
			long t0 = System.currentTimeMillis();
			colliding = true;
			collisions.clear();
			colP.clear();
			int c = 0;
			if(polygons.size()>0){
				for(Polygon p:polygons){
					p.rotate(0.1f);
					for(Polygon p2:polygons){
						if(p!=p2){
							//handler.onCollisionDetected(p,p2);
							collisions.add(new Collision(p, p2));
							//colP.add(p.getIntersection(p2));
							//colliders[c++ % colliders.length].add(new Collision(p, p2));
						}
					}
				}
			}	
			//System.out.println("Collide in "+(System.currentTimeMillis()-t0));
			colliding = false;
		}
	}
	
//	public void collide2(){
//		long t0 = System.currentTimeMillis();
//		colliding = true;
//		collisions.clear();
//		colP.clear();
//		final List<Polygon> _polygons = new ArrayList<Polygon>(polygons);
//		
//		if(polygons.size()>0){
//			for(Polygon p:polygons){
//				p.rotate(0.1f);
//			}
//			
//			for(int c=0;c<polygons.size();c++){
//				final Polygon _self = polygons.get(c);
//				executor.execute(new Runnable() {
//					
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						for(Polygon p2:_polygons){
//							if(_self!=p2){
//								//handler.onCollisionDetected(p,p2);
//								collisions.add(new Collision(_self, p2));
//								//colP.add(p.getIntersection(p2));
//							}
//						}
//					}
//				});
//			}
//		}	
//		
//		//System.out.println("Collide in "+(System.currentTimeMillis()-t0));
//		colliding = false;
//	}
	public void onCollision(Polygon p1, Polygon p2){
		
	}
	
	public List<Polygon> getPolygons(){
		return polygons;
	}

	public List<Collision> getCollisions(){
		synchronized (this) {
			return new ArrayList<Collision>(collisions);
		}
		
	}
	
	public List<List<Point>> getColP(){
		synchronized (this) {
			return new ArrayList<List<Point>>(colP);
		}
	}
}
